﻿using System;
using System.Windows;
using System.Windows.Controls;

using MemoryGames.Helpers;

namespace MemoryGames.Games.Memory
{
    /// <summary>
    /// Interaction logic for Maintenance.xaml
    /// </summary>
	public partial class Maintenance : UserControl, IGameControl
    {
        #region Events
        public event MainForm.MainFormRestartDelegate MainFormRestart;
        #endregion

        #region Member Variables
        #endregion

        #region Constructors
        public Maintenance()
        {
            InitializeComponent();
        }
        #endregion

        #region Methods

		/// <summary>
		/// Perform initialization stuff here
		/// </summary>
		public bool Initialize()
		{
			Background = null;

			txtImagePath.Text = Configuration.Settings.Load("Memory", "ImagePath");
			
			switch(Configuration.Settings.Load("Memory", "NumberOfVerticalCards").ParseIntDefault(4))
			{
				case 2:
					rbCol2.IsChecked = true;
					break;
				case 4:
					rbCol4.IsChecked = true;
					break;
				case 6:
					rbCol6.IsChecked = true;
					break;
				case 8:
					rbCol8.IsChecked = true;
					break;
				default:
					rbCol4.IsChecked = true;
					break;
			}

			return true;
		}

    	#endregion

        #region Event Handlers

        private void btnBrowseImagePath_Click(object sender, RoutedEventArgs e)
        {
            //GK Let user select an image folder
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.SelectedPath = txtImagePath.Text;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtImagePath.Text = fbd.SelectedPath;
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Configuration.Settings.Save("Memory", "ImagePath", txtImagePath.Text);

        	int numberOfCards = 4;
			if (rbCol2.IsChecked.HasValue && rbCol2.IsChecked.Value) numberOfCards = 2;
			if (rbCol4.IsChecked.HasValue && rbCol4.IsChecked.Value) numberOfCards = 4;
			if (rbCol6.IsChecked.HasValue && rbCol6.IsChecked.Value) numberOfCards = 6;
			if (rbCol8.IsChecked.HasValue && rbCol8.IsChecked.Value) numberOfCards = 8;

			Configuration.Settings.Save("Memory", "NumberOfHorizontalCards", numberOfCards.ToString());
			Configuration.Settings.Save("Memory", "NumberOfVerticalCards", numberOfCards.ToString());

            this.MainFormRestart.Invoke();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.MainFormRestart.Invoke();
        }

        #endregion

    }
}